package team.you.ticket.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.you.ticket.dao.TicketLogMapper;
import team.you.ticket.pojo.*;
import team.you.ticket.service.TicketLogService;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class TicketLogServiceImpl implements TicketLogService
{
    @Autowired
    private TicketLogMapper ticketLogMapper;
    @Override
    public Result getAllLog(Integer page, Integer limit) {
        Result result = new Result();
        if (page <= 0) {
            page = 1;
            page = (page - 1) * limit;
        } else page = (page - 1) * limit;
        List<TicketLog> ticketLogs = ticketLogMapper.selectAll(page, limit);
        Integer count = ticketLogMapper.getCount();
        result.setData(ticketLogs);
        result.setCount(count);
        return result;
    }

    @Override
    public Result selectByLike(String value, Integer page, Integer limit) {
        Result result = new Result();
        if (page <= 0) {
            page = 1;
            page = (page - 1) * limit;
        } else page = (page - 1) * limit;
        List<TicketLog> ticketLogs = ticketLogMapper.selectByLike(value,page, limit);
        result.setData(ticketLogs);
        result.setCount(ticketLogs.size());
        return result;
    }

    @Override
    public Result echarts() {
        Result result=new Result();
        List<EchartsList> data=ticketLogMapper.selectEcharts();
        if (data!=null)
        {
            for(int i=0;i<data.size();i++)
            {
                String str=data.get(i).getName();
                data.get(i).setName(str.substring(0,10));
            }
            result.setMsg("获取成功");
            result.setData(data);
            return result;
        }
        else
        {
            result.setCode(500);
            result.setMsg("获取失败");
            return result;
        }
    }

    @Override
    public Result progress() {
        Result result=new Result();
        Integer i=ticketLogMapper.selectByAll();
        Integer j=ticketLogMapper.selectByRemarks();
        DecimalFormat df=new DecimalFormat("#.##");
        Double aDouble=Double.valueOf(df.format(j/(i*1.0)));
        Integer pay=(int) (aDouble*100);
        ReturnProgress returnProgress=new ReturnProgress();
        returnProgress.setPay(pay);
        List<ReturnProgress> list=new ArrayList<ReturnProgress>();
        list.add(0,returnProgress);
        result.setData(list);
        return result;
    }
}
